package com.cmwa.scbp.base.config.thread;

import java.util.concurrent.ThreadPoolExecutor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class ExecutorConfig {
	private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);
	
	/**
	 * 系统其它任务处理线程池
	 */
	@Bean("asyncExecutor")
	@Primary
	public ThreadPoolTaskExecutor asyncServiceExecutor(){
		logger.info("ThreadPool asyncExecutor init ... ");
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(10);
		executor.setMaxPoolSize(15);
		executor.setKeepAliveSeconds(3000);
		executor.setQueueCapacity(1000);
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.setThreadNamePrefix("asyncExecutor-");
		executor.initialize();
		logger.info("ThreadPool asyncExecutor init complete !");
		return executor;
	}
	
	
	/**
	 * 统一定时调度平台，处理线程池
	 */
	@Bean("timeTaskThreadPoolExecutor")
	public ThreadPoolTaskExecutor timeTaskServiceExecutor(){
		logger.info("ThreadPool timeTaskExecutor init ... ");
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(5);
		executor.setMaxPoolSize(10);
		executor.setKeepAliveSeconds(3000);
		executor.setQueueCapacity(1000);
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.setThreadNamePrefix("timeTaskExecutor-");
		executor.initialize();
		logger.info("ThreadPool timeTaskExecutor init complete !");
		return executor;
	}
	
	
	/**
	 * 计算指标任务处理线程池
	 */
	@Bean("quotaThreadPoolExecutor")
	public ThreadPoolTaskExecutor quotaServiceExecutor(){
		logger.info("ThreadPool quotaExecutor init ... ");
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(10);
		executor.setMaxPoolSize(15);
		executor.setKeepAliveSeconds(3000);
		executor.setQueueCapacity(100000);
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		executor.setThreadNamePrefix("quotaExecutor-");
		executor.initialize();
		logger.info("ThreadPool quotaExecutor init complete !");
		return executor;
	}
	
}
